from selenium import webdriver
from selenium.webdriver.edge.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.options import Options
import time


def login_browser():
    # 设置浏览器驱动路径
    driver_path = "msedgedriver.exe"  # 替换为你的 ChromeDriver 路径

    # 配置浏览器选项
    chrome_options = Options()
    # chrome_options.add_argument("--start-maximized")  # 窗口最大化

    # 初始化 WebDriver
    service = Service(executable_path=driver_path)
    driver = webdriver.Chrome(service=service, options=chrome_options)

    print("正在尝试访问页面...")
    # 模拟登录逻辑
    # 这里可以替换为实际的登录代码，例如使用Selenium等
    raise Exception("登录失败：用户名或密码错误")  # 模拟登录失败的异常

    # 打开百度网页
    driver.get("https://game.qzone.qq.com/353?appid=353&uin=1412158641&pfid=2&qz_ver=8&appcanvas=0&qz_style=35&params=&entertime=1739265774588&canvastype=home&via=QZ.MYAPP.CASUAL&cdn_use_https=1&detail=&app_custom=&app_isfirst=1")
    # driver.switch_to.frame('login_frame')
    # driver.find_element(By.ID, 'switcher_plogin').click()
    # qq_number = '1412158641'
    # password = 'Cyh111111'
    # driver.find_element(By.ID, 'u').send_keys(qq_number)
    # driver.find_element(By.ID, 'p').send_keys(password)
    #
    # # 点击登录按钮
    # driver.find_element(By.ID, 'login_button').click()
    # 等待登录成功
    time.sleep(60)

    try:
        add_friend_buttons = driver.find_elements(By.ID, 'add_friend_button')
        for button in add_friend_buttons:
            button.click()
            time.sleep(1)  # 避免操作过快
    except Exception as e:
        print(f"添加好友时出错: {e}")

    # 等待页面加载完成
    driver.implicitly_wait(10)  # 设置隐式等待时间

    # 打印当前页面标题
    print("当前页面标题：", driver.title)

    # 添加暂停逻辑，等待用户手动关闭浏览器
    print("浏览器已打开，按回车键关闭浏览器...")
    input()  # 等待用户按下回车键

    # 关闭浏览器
    driver.quit()